<?php
namespace WebApper;
/*
 * [leadPayment]
 *
 */
class LeadPayment extends  \WebApper\Shortcode {
	/**
     * Define shortcode properties
     *
     */
	protected $shortcode = 'leadPayment';
	
	/**
     * Initialize the Shortcode Class
     *
     */
	public function __construct() {
		add_shortcode( $this->shortcode, array($this, 'shortcode') );
		add_action( 'wp_ajax_web_apper' . $this->shortcode, array($this, 'sendAjaxResponse') );
		add_action( 'wp_ajax_nopriv_web_apper' . $this->shortcode, array($this, 'sendAjaxResponse') );	
	}

    /**
     * Handles the add post shortcode
     *
     * @param array $atts
     */
    public function shortcode( $atts ) {
		global $wpdb, $webapper;
		 $user_payment_transaction = $webapper->tables->user_payment_transaction; 
		// Get the Custom Post Type
		  $get_posts_args = array( 'post_type' => 'lead', 'order'=> 'ASC', 'orderby' => 'title', 'posts_per_page' => -1 );  // Set query arguments
		  $customPostObjs = get_posts( $get_posts_args );  // Get the posts
		  //print_r($customPostObjs);
		  foreach($customPostObjs as $post)
		  {							
		  	//echo "<br>".$post->ID;
			$post_id = $post->ID;
		  }
		// Check for required shortcode attributes
		$msg = $this->has_req_attrs( $atts );
		if ( $msg !== true )
			return $msg;			
		
			$get_post_meta      = get_post_meta($post_id);
			$email_address 		= $get_post_meta['email-primary'][0];
			$exist_user_id = email_exists($email_address);
		
		    $payment_master = web_apper_get_payment_master($exist_user_id);
	        $user_payment_master_id = $payment_master ->user_payment_master_id;
			$card_holderName    	= $payment_master ->card_holder_name;
			list($fname, $lname)    = explode(' ',$card_holderName,2);
			$card_type         	    = $payment_master ->card_type;
			$card_number        	= $payment_master ->card_number;
			$cardExpire_month   	= $payment_master ->card_expiration_month;
			$cardExpire_year   	    = $payment_master ->card_expiration_year;
			$year                   = substr($cardExpire_year,2);
		    $date 				    = $cardExpire_month.$year;
			$country            	= $payment_master ->card_country;
			$address            	= $payment_master ->card_address;
			$city               	= $payment_master ->card_city;
			$state              	= $payment_master ->card_state;
			$card_ccv           	= $payment_master ->card_ccv;
			$zip_code  				= $payment_master ->card_zip_code;
			$bank_routing_number	= $payment_master ->bank_routing_number;
			$account_number 		= $payment_master ->bank_account_number;
			$check_number 			= $payment_master ->bank_check_number;
		    $post_url = AUTHORIZED_NET_URL;
		    $post_values = array(
			// the API Login ID and Transaction Key must be replaced with valid values
			"x_login"			=> AUTHORIZED_NET_LOGIN,
			"x_tran_key"		=> AUTHORIZED_NET_TRAN_KEY,
		    "x_version"			=> "3.1",
			"x_delim_data"		=> "TRUE",
			"x_delim_char"		=> "|",
			"x_relay_response"	=> "FALSE",
		    "x_type"			=> "AUTH_CAPTURE",
			"x_method"			=> "CC",
			"x_fp_timestamp"    => $tstamp,
			"x_card_num"		=> $card_number,
			"x_exp_date"		=> $date,
		    "x_amount"			=> "100",
			"x_description"		=> "A Sample Transaction",
			"x_bankName"        =>"State Bank",
			"echeckType"        =>"Web",
		    "x_first_name"		=> $fname,
			"x_last_name"		=> $lname,
			"x_country"	        => $country,
			"x_address"			=> $address,
			"x_city"			=> $city,
			"x_state"			=> $state,
			"x_zip"				=> $zip_code,
			"x_email"           => "ranjan.bera@snapstech.com",
			"x_email_customer"  =>"",
			"x_header_email_receipt" =>"Payment Processing",
			"x_ship_to_address"	    => "kolkata,Garia",
			"x_tax"                 => "Tax1<|>payment tax<|>0.09",
			"submitTimeUTC"         =>$timeStamp
			// Additional fields can be added here as outlined in the AIM integration
			// guide at: http://developer.authorize.net
		);
		$data = GetUrlValue($post_values, $post_url);
		//print_r($data);
		$user_payment_master_table = $webapper->tables->user_payment_master;
		echo "<table>";
		foreach($data as $dataRow)
		{
			echo "<tr>";
			for($i=0;$i<=67;$i++)
			{
				echo "<td>".$dataRow[$i]."</td>";
				
			}
			echo "</tr>";
		}
		echo "</table>";
		
		
		/*echo "<OL>\n";
    foreach ($data as $value)
    {
      echo "<LI>" . $value . "&nbsp;</LI>\n";
      $i++;
    }
    echo "</OL>\n";*/
			$sql = $wpdb->prepare( "INSERT INTO {$user_payment_transaction} ( user_id,amount_paid,transaction_number ) VALUES ( %d,%f,%s)", $exist_user_id, '100',$data['6']) ;
     $result = $wpdb->query( $sql );
	 if($result)
	 {
		 echo "The Transaction Successfully done"; 
	 }
	
	
	}
}
$initialize = new LeadPayment(); 
?>